export function TreeToFlat(data: Array<any>, childrenKey: string): Array<any> {
  // data树状数据源，childrenKey子节点属性名
  const flat = []
  let tree: Array<any> = []
  tree = tree.concat(data)
  while (tree.length) {
    const firstItem = tree.shift()
    if (firstItem[childrenKey]) {
      tree = tree.concat(firstItem[childrenKey])
      delete firstItem[childrenKey]
    }
    flat.push(firstItem)
  }
  return flat
}

export function FlatToTree(data: Array<any>, parentKey: string): Array<any> {
  // data树状数据源，parentKey父节点属性名
  const tree = []
  type mapType = {
    [key: string]: any
  }
  const map: mapType = {}
  for (const i in data) {
    map[data[i].id] = data[i]
    data[i].children = []
  }

  for (const node of data) {
    if (node[parentKey] !== null) {
      map[node[parentKey]].children.push(node)
    } else {
      tree.push(node)
    }
  }

  return tree
}
